<?php 
namespace app\admin\controller;

use app\admin\model\CommentModel;
use app\admin\model\MemberModel;
use app\admin\model\ReplyModel;
use app\admin\model\NewsModel;

/**
 * 评论管理
 */
class Comment extends Base
{
	
	public function index()
	{
		$comment = new CommentModel();
		$member  = new MemberModel();
		$reply   = new ReplyModel();

		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");
		$title    = input("param.title/s");
		$nickname = input("param.nickname/s");

		if ($type == 1) {
			$order    = "create_time desc";
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;

			$where = [];
			if (!empty($title)) {
				$where[] = "title LIKE '%".$title."%'";
			}
			if (!empty($nickname)) {

				$wherearr = "nickname LIKE '%".$nickname."%'";
				$userlist = $member->get_list($wherearr, "user_id asc", "user_id");
				if (!empty($userlist)) {
					$userids = [];
					foreach ($userlist as $k => $v) {
						$userids[] = $v['user_id'];
					}
				}

				$where[] = "user_id IN (".implode(",", $userids).")";
			}

			if (count($where) > 0) {
				$wherestr = implode(" AND ", $where);
			}

			// 统计
			$sum  = $comment->get_count($wherestr);
			// 获取数据
			$list = $comment->get_list_page($wherestr, $order, $fields = "*", $start, $size);
			foreach ($list as $k => $v) {
				// 昵称
				$list[$k]['nickname'] = $member->get_field(['user_id' => $v['user_id']], "nickname");
				// 讨论
				$list[$k]['discuss'] = $reply->get_count(['comment_id' => $v['comment_id']]);
				
			}


            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $list;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('index');
	}
	
	// 删除
	public function delcomment()
	{
		$comment = new CommentModel();
		$reply   = new ReplyModel();

		$id      = input("param.comment_id/d");
		$ids     = input("param.ids");

		if (!empty($id)) {
			$where = ['comment_id' => $id];
			
			$count = $reply->get_count($where);
			if ($count > 0) {
				echo json_encode(msg_error("评论有回复信息，删除失败"));
				exit;
			}
		}

		if (!empty($ids)) {
			$ids   = json_decode($ids);
			$where = "comment_id IN (".implode(",", $ids).")";

			$count = $reply->get_count($where);
			if ($count > 0) {
				echo json_encode(msg_error("评论有回复信息，删除失败"));
				exit;
			}			
		}

		// 删除数据
		$res = $comment->del_data($where);
		if (isset($res['msg'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}

	// 查看
	public function detail()
	{
		$comment    = new CommentModel();
		$comment_id = input("param.comment_id/d");

		$where = ['comment_id' => $comment_id];
		$data  = $comment->get_info($where);
		// 改变查看状态
		if ($data['read'] == -1) {
			// 获取数据
			$condtion = [
				'comment_id' => $comment_id,
				'read'       => 1,
			];
			$res  = $comment->save_data($condtion);

			if (isset($res['msg'])) {
				echo $res['msg'];exit;
			}
		}
		return $this->fetch('edit', ['data' => $data]);
	}


	// 修改状态
	public function change_status()
	{
		$comment = new CommentModel();

		if (request()->isAjax()) {

			// 获取数据
			
			$status = input("param.status/d");
			$id     = input("param.id/d");
			$data   = [
				'comment_id' => $id,
				'status'	 => $status,
			];

			$res  = $comment->save_data($data);
			if (isset($res['msg'])) {
				echo json_encode($res);exit;
			}
			
			echo json_encode(msg_success("编辑成功"));
			exit;
		}
	}

	public function discuss()
	{
		$comment = new CommentModel();
		$member  = new MemberModel();
		$reply   = new ReplyModel();

		$comment_id = input("param.comment_id/d");
		$type     = input("param.type/d");
		$pagenum  = input("param.page/d") ;
		$pagesize = input("param.limit/d");
		$rtype    = input("param.rtype/d");
		$author = input("param.author/s");
		$goal = input("param.goal/s");

		if ($type == 1) {
			$order    = "create_time desc";
			$pagenum  = $pagenum < 1 ? 1: $pagenum;
			$pagesize = $pagesize < 1 ? 10: $pagesize;
			$start    = ($pagenum - 1) * $pagesize;
			$size     = $pagesize;

			$where   = [];
			// 评论ID
			$where[] = "comment_id = ".$comment_id;

			if (!empty($rtype)) {
				$where[] = "reply_type = ".$rtype;
			}
			// 作者匹配
			if (!empty($author)) {

				$wherearr = "nickname LIKE '%".$author."%'";
				$userlist = $member->get_list($wherearr, "user_id asc", "user_id");
				if (!empty($userlist)) {
					$userids = [];
					foreach ($userlist as $k => $v) {
						$userids[] = $v['user_id'];
					}
				}

				$where[] = "user_id IN (".implode(",", $userids).")";
			}

			// 目标匹配
			if (!empty($goal)) {

				$wherearr = "nickname LIKE '%".$goal."%'";
				$userlist = $member->get_list($wherearr, "user_id asc", "user_id");
				if (!empty($userlist)) {
					$userids = [];
					foreach ($userlist as $k => $v) {
						$userids[] = $v['user_id'];
					}
				}

				$where[] = "reply_pid IN (".implode(",", $userids).")";
			}

			if (count($where) > 0) {
				$wherestr = implode(" AND ", $where);
			}

			// 统计
			$sum  = $reply->get_count($wherestr);
			// 获取数据
			$list = $reply->get_list_page($wherestr, $order, $fields = "*", $start, $size);
			foreach ($list as $k => $v) {
				// 昵称
				$list[$k]['nickname'] = $member->get_field(['user_id' => $v['user_id']], "nickname");
				// 目标
				$list[$k]['reply_name'] = $member->get_field(['user_id' => $v['reply_pid']], "nickname");
				// 类型
				$list[$k]['type'] = ($v['reply_type'] === 1)?"评论":"回复"; 
			}


            $data = array();
            $data['code']   = '0';
            $data['msg']    = '';
            $data['count']  = $sum;
            $data['data']   = $list;

            echo json_encode($data);
            exit;
		}

		return $this->fetch('discuss', ['comment_id' => $comment_id]);
	}

	// 查看回复
	public function reply_info()
	{
		$reply   = new ReplyModel();
		$reply_id = input("param.reply_id/d");

		$where = ['reply_id' => $reply_id];
		$data  = $reply->get_info($where);

		return $this->fetch('edit', ['data' => $data]);
	}

	// 修改回复状态
	public function change_reply()
	{
		$reply   = new ReplyModel();
		if (request()->isAjax()) {

			// 获取数据
			
			$status = input("param.status/d");
			$id     = input("param.id/d");
			$data   = [
				'reply_id' => $id,
				'status'	 => $status,
			];

			$res  = $reply->save_data($data);
			if (isset($res['msg'])) {
				echo json_encode($res);exit;
			}
			
			echo json_encode(msg_success("编辑成功"));
			exit;
		}
	}

	// 删除回复
	public function delreply()
	{
		$reply   = new ReplyModel();

		$reply_id   = input("param.reply_id/d");
		$ids        = input("param.ids");

		if (!empty($id)) {

			$where = ['reply_id' => $reply_id];
		}

		if (!empty($ids)) {
			$ids   = json_decode($ids);
			$where = "reply_id IN (".implode(",", $ids).")";
		}

		// 删除数据
		$res = $reply->del_data($where);
		if (isset($res['msg'])) {
			echo json_encode($res);
		}else{
			echo json_encode(msg_success("删除成功"));
		}
		exit;
	}
}


 ?>